package com.yokit.c2c.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yokit.c2c.pojo.Goods;
import com.yokit.c2c.pojo.Order;
import com.yokit.c2c.response.ResponseObject;
import com.yokit.c2c.service.GoodsService;
import com.yokit.c2c.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author fengzeng
 * @create 2021/6/14 17:23
 */
@RestController
public class OrderController {

  @Resource
  private OrderService orderService;

  @Resource
  private GoodsService goodsService;

  /**
   * 创建订单
   * @param request
   * @return
   */
  @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
  public ResponseObject createOrder(@RequestBody Map<String, String> request) {

    Order order = new Order();
    order.setOrderState("0");

    order.setCreateTime(new Date());

    /**
     * 获取下单用户id
     */
    Integer userId = Integer.valueOf(request.get("userId"));
    order.setUserId(userId);

    /**
     * 收货电话
     */
    Integer mobile = Integer.valueOf(request.get("mobile"));
    order.setMobile(mobile);

    /**
     * 收货地址
     */
    String address = request.get("address");
    order.setAddress(address);

    /**
     * 获取商品id
     */
    Integer goodsId = Integer.valueOf(request.get("goodsId"));
    Goods goods = goodsService.selectByGoodsId(goodsId);

    /**
     * 把商品信息存放在订单中
     */
    order.setShopId(goods.getShopId());
    order.setPrice(goods.getGoodsPrice());
    order.setGoodsId(goods.getGoodsId());


    orderService.insertOrder(order);
    return new ResponseObject(200, "下单成功");
  }

  /**
   * 获取订单
   * @param userId
   * @param pageSize
   * @param pageNum
   * @param state
   * @return
   */
  @RequestMapping(value = "/order/{userId}/{state}", method = RequestMethod.GET)
  public Map getOrderListByUserId(@PathVariable("userId") String userId,
                                  @RequestParam("pageSize") String pageSize,
                                  @RequestParam("pageNum") String pageNum,
                                  @PathVariable(value = "state",required = false) String state) {


    Map<String, Object> response = new HashMap<>();
    List<Order> orders = new ArrayList<>();


    int pageSizeInt = Integer.parseInt(pageSize);
    int pageNumInt = Integer.parseInt(pageNum);


    //获取用户id
    Integer userIdInt = Integer.parseInt(userId);

    //开启分页
    PageHelper.startPage(pageNumInt, pageSizeInt);
    if (state.equals("-1")) {
      orders = orderService.selectByUserId(userIdInt);
    }else {
      orders = orderService.selectByUserIdAndState(userId, state);

    }
    PageInfo pageInfo = new PageInfo(orders);

    long totalPages = pageInfo.getTotal();
    response.put("status", 200);
    response.put("msg", "获取商品成功");
    response.put("total", totalPages);
    response.put("data", pageInfo.getList());
    return response;

  }

  /**
   * 修改订单状态
   * @param orderId
   * @param state
   * @return
   */
  @RequestMapping(value = "/order/{orderId}/{state}", method = RequestMethod.PUT)
  public ResponseObject confirmOrder(@PathVariable String orderId, @PathVariable String state) {

    Integer orderIdInt = Integer.parseInt(orderId);

    Order order = orderService.selectByOrderId(orderIdInt);


    order.setOrderState(state);

    orderService.updateByPrimaryKeySelective(order);
    return new ResponseObject(200, "更新订单状态成功", order);
  }



}
